const express = require("express");
const app = express();
const cors = require("cors");
//处理跨域
app.use(
  cors({
    origin(origin, callback) {
      if (!origin) {
        callback(null, "*");
        return;
      }
      callback(null, origin);
    },
    credentials: true,
  })
);

//映射public目录中的静态资源
const path = require("path");
const staticRoot = path.resolve(__dirname, "./assets");
app.use(express.static(staticRoot));



//解析cookie
var cookieParser = require('cookie-parser')
app.use(cookieParser());

// 解析 application/x-www-form-urlencoded 格式的请求体
app.use(express.urlencoded({ extended: true }));

// 解析 application/json 格式的请求体
app.use(express.json());

// 处理 api 的请求
app.use("/api", require("./route/init"));

//处理错误的中间件
app.use(require("./route/middleware/errorMiddleware"));

const port = 5008;
app.listen(port, () => {
  console.log(`server listen on ${port}`);
});
